package servlet;

import bean.Result;
import biz.BizException;
import biz.PhoneBiz;
import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.Type;

// zpl
@WebServlet(name = "SavePhoneServlet", value = "/SavePhoneServlet.s")
public class SavePhoneServlet extends HttpServlet {
    private PhoneBiz biz = new PhoneBiz();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String product = request.getParameter("product");
        String price = request.getParameter("price");
        String count = request.getParameter("count");
        String type = request.getParameter("type");
        String intro = request.getParameter("intro");
        String image = request.getParameter("image");
        String color = request.getParameter("color");
        String id = request.getParameter("id");

        Result result;

        try {
            if (id != null) {
                biz.modify(id, product, price, count, type,intro, image, color);
            }else {
                biz.create(product, price, count, type,intro, image, color);
            }
            result = new Result(1, "商品保存成功!", null);
        } catch (BizException e) {
            e.printStackTrace();
            result = new Result(0, e.getMessage(), null);
        }
        String json = new Gson().toJson(result);
        response.getWriter().append(json);
    }
}
